﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TLMFKART 
    {
        #region ==CGRP===========
        public static readonly DependencyProperty CGRPProperty = DependencyProperty.Register("CGRP",
            typeof(string), typeof(TLMFKART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCGRPChanged));
        public string CGRP
        {
            get
            {
                return ((VLMFKART)MV).CGRP;
            }
            set
            {
                SetValue(CGRPProperty, value);
                ((VLMFKART)MV).CGRP = value;
            }
        }
        private static void OnCGRPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TLMFKART;
            if (src != null)
            {
                src.CGRP = (string)e.NewValue;
                src.CGRPCHG();
            }
        }
        #endregion
     
        public TLMFKART()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VLMFKART();
            this.DataContext = MV;  
            InitializeComponent(); 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
         
        }
        
        
        int option = 1;
     
        public void CGRPCHG()
        {
            if (((VLMFKART)MV).dwLM == null)
                return;
            TMV.LMFPREISGRP = CGRP;
             
        }
        public void NEWLM(MessageInfo msg)
        {
            try
            {
                dwData = TBMain.NewRow();
                ((VLMFKART)MV).SetUpNewRow(ref dwData);
                dwData["LMFMANDANT"] = app.Mandant;
                if (msg == null)
                {
                    dwData["LMFKARTNR"] = CARDNO;
                    dwData["LMFFARBE"] = FARBE;
                }
                else
                {
                    dwData["LMFKARTNR"] = msg.ART;
                    dwData["LMFFARBE"] = msg.FARBE;
                } 
                Insert(dwData);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Insert(DataRow _dwLM)
        {

            _dwLM["LMFNEUBEN"] = app.User;
            _dwLM["LMFNEUDAT"] = vm.date;
            TMV.InsertLMFKART(_dwLM, app.company);
            TBMain.Rows.InsertAt(_dwLM, 0);
            vwMaster.View.MoveCurrentToFirst();

        }
        public void UPDATELM(DataRow _dwLM)
        {
            if (_dwLM == null)
                return;
            TMV.dwLM["LMFAENDBEN"] = app.User;
            TMV.dwLM["LMFAENDDAT"] = TMV.date;
            TMV.UpdateLMFKART(_dwLM, 0);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["LMFMANDANT"] = app.Mandant;
            dwData["LMFKARTNR"] = ART;
            SResult =TMV.QueryLMFKART(dwData, 2);
            MergeData(SResult, false);
            base.QueryCommand(sender, e); 
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            NEWLM(null);
            base.NewCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (((VLMFKART)MV).FARBE.Trim().Length == 0)
            {
                MessageBox.Show("请先输入色号");
                return;
            }
             if (dwMaster == null)
                return;
            
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["LMFFARBE"] = FARBE;
            Insert(dwData);
             FARBE = "";
            base.CopyCommand(sender, e);
        }
        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                base.SaveCommand(sender, e);
                UPDATELM(dwMaster);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


    }
}
